package com.gfxy.modules.statistics.controller;

import com.alibaba.fastjson.JSONObject;
import com.gfxy.modules.statistics.service.CoreTotalScoreExcel;
import com.gfxy.modules.statistics.vo.CoreSummaryForSchoolVo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.jeecgframework.poi.excel.view.MiniAbstractExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;


@Controller("coreTotalScoreForSchoolExcelView")
public class CoreTotalScoreForSchoolExcelView extends MiniAbstractExcelView {
    public static final String FILE_NAME = "fileName";
    public static final String TITLE = "title";
    public static final String CONTENT = "content";

    public static final String CONTENT_1 = "content-1";

    @Autowired
    private CoreTotalScoreExcel coreTotalScoreExcel;

    public CoreTotalScoreForSchoolExcelView() {
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String codedFileName = "临时文件";


        Workbook workbook =  new SXSSFWorkbook();

        CellStyle style = workbook.createCellStyle();
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);

        CellStyle cellstyle2 = workbook.createCellStyle();
        cellstyle2.setVerticalAlignment(VerticalAlignment.CENTER);
        cellstyle2.setAlignment(HorizontalAlignment.CENTER);
        cellstyle2.setBorderTop(BorderStyle.THIN);
        cellstyle2.setBorderBottom(BorderStyle.THIN);
        cellstyle2.setBorderLeft(BorderStyle.THIN);
        cellstyle2.setBorderRight(BorderStyle.THIN);

        Sheet sheet = workbook.createSheet("各分院总分");
        String title = (String) model.get(TITLE);

        for(int i=0 ; i<=1; i++){
            Row row = sheet.createRow(i);
            for(int j = 0 ; j <= 6 ;j ++){
                Cell cell = row.createCell(j);
                cell.setCellValue(title);
                cell.setCellStyle(cellstyle2);
            }
        }
        CellRangeAddress region0 = new CellRangeAddress(0, 1, 0, 6);
        sheet.addMergedRegion(region0);

        this.createSubTitle1(sheet, style);
        List<CoreSummaryForSchoolVo> coreSummaryForSchoolVoList = (List<CoreSummaryForSchoolVo>) model.get(CONTENT);


        int rowIndex = 3;
        for (int i = 0; i < coreSummaryForSchoolVoList.size(); i++) {
            CoreSummaryForSchoolVo coreSummaryForSchoolVo = coreSummaryForSchoolVoList.get(i);
            Row contentRow = sheet.createRow(rowIndex);

            Cell numCell = contentRow.createCell(0);
            numCell.setCellValue(i+1);
            numCell.setCellStyle(style);

            String orgName = coreSummaryForSchoolVo.getOrgName();
            Cell orgNameCell = contentRow.createCell(1);
            orgNameCell.setCellValue(orgName);
            orgNameCell.setCellStyle(style);

            double teachingBuild = coreSummaryForSchoolVo.getTeachingBuild();
            Cell teachingBuildCell = contentRow.createCell(2);
            teachingBuildCell.setCellValue(teachingBuild);
            teachingBuildCell.setCellStyle(style);

            double studentMatch = coreSummaryForSchoolVo.getStudentMatch();
            Cell studentMatchCell = contentRow.createCell(3);
            studentMatchCell.setCellValue(studentMatch);
            studentMatchCell.setCellStyle(style);

            double research = coreSummaryForSchoolVo.getResearch();
            Cell researchCell = contentRow.createCell(4);
            researchCell.setCellValue(research);
            researchCell.setCellStyle(style);

            double studentManage = coreSummaryForSchoolVo.getStudentManage();
            Cell studentManageCell = contentRow.createCell(5);
            studentManageCell.setCellValue(studentManage);
            studentManageCell.setCellStyle(style);

            double totalScore = coreSummaryForSchoolVo.getTotalScore();
            Cell totalScoreCell = contentRow.createCell(6);
            totalScoreCell.setCellValue(totalScore);
            totalScoreCell.setCellStyle(style);
            rowIndex = rowIndex+1;
        }

        Sheet sheet1 = workbook.createSheet("发展关键核心指标全校教师积分汇总");
        List<JSONObject> contents = (List<JSONObject>) model.get(CONTENT_1);
        coreTotalScoreExcel.createSheet(workbook,sheet1,"发展关键核心指标全校教师积分汇总" ,contents);

        if (model.containsKey(FILE_NAME)) {
            codedFileName = (String)model.get(FILE_NAME);
        }

        codedFileName = codedFileName + ".xls";

        if (this.isIE(request)) {
            codedFileName = URLEncoder.encode(codedFileName, "UTF8");
        } else {
            codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
        }


        response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
    }

    private void createSubTitle1(Sheet sheet, CellStyle style) {



        Row rowSubTitle1 = sheet.createRow(2);

        Cell numCell = rowSubTitle1.createCell(0);
        numCell.setCellValue("序号");
        numCell.setCellStyle(style);

        Cell teacherNumCell = rowSubTitle1.createCell(1);
        teacherNumCell.setCellValue("学院");
        teacherNumCell.setCellStyle(style);

        Cell teacherNameCell = rowSubTitle1.createCell(2);
        teacherNameCell.setCellValue("教学建设");
        teacherNameCell.setCellStyle(style);

        Cell depName = rowSubTitle1.createCell(3);
        depName.setCellValue("学生大赛");
        depName.setCellStyle(style);

        Cell titleRankCell = rowSubTitle1.createCell(4);
        titleRankCell.setCellValue("科学研究与社会服务");
        titleRankCell.setCellStyle(style);

        Cell classTimeCell = rowSubTitle1.createCell(5);
        classTimeCell.setCellValue("基层党建与学生管理");
        classTimeCell.setCellStyle(style);


        Cell totalCell = rowSubTitle1.createCell(6);
        totalCell.setCellValue("总分");
        totalCell.setCellStyle(style);

    }

    private void createSubTitle2(Sheet sheet, CellStyle style) {
        Row rowSubTitle2 = sheet.createRow(3);


        Cell tempCell = rowSubTitle2.createCell(0);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);
        tempCell = rowSubTitle2.createCell(1);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);
        tempCell = rowSubTitle2.createCell(2);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);
        tempCell = rowSubTitle2.createCell(3);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);
        tempCell = rowSubTitle2.createCell(4);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);

        Cell classTimePointCell = rowSubTitle2.createCell(5);
        classTimePointCell.setCellValue("指标");
        classTimePointCell.setCellStyle(style);

        Cell classTimeScoreCell = rowSubTitle2.createCell(6);
        classTimeScoreCell.setCellValue("积分");
        classTimeScoreCell.setCellStyle(style);

        Cell classTimeBuildPointCell = rowSubTitle2.createCell(7);
        classTimeBuildPointCell.setCellValue("指标");
        classTimeBuildPointCell.setCellStyle(style);



        Cell teachingBuildScoreCell = rowSubTitle2.createCell(8);
        teachingBuildScoreCell.setCellValue("积分");
        teachingBuildScoreCell.setCellStyle(style);


        Cell honourScoreCell = rowSubTitle2.createCell(9);
        honourScoreCell.setCellValue("积分");
        honourScoreCell.setCellStyle(style);

        Cell researchPointCell = rowSubTitle2.createCell(10);
        researchPointCell.setCellValue("指标");
        researchPointCell.setCellStyle(style);


        Cell researchScoreCell = rowSubTitle2.createCell(11);
        researchScoreCell.setCellValue("积分");
        researchScoreCell.setCellStyle(style);

        Cell educationPointCell = rowSubTitle2.createCell(12);
        educationPointCell.setCellValue("指标");
        educationPointCell.setCellStyle(style);

        Cell educationScoreCell = rowSubTitle2.createCell(13);
        educationScoreCell.setCellValue("积分");
        educationScoreCell.setCellStyle(style);

        Cell totalPointCell = rowSubTitle2.createCell(14);
        totalPointCell.setCellValue("指标");
        totalPointCell.setCellStyle(style);

        Cell totalScoreCell = rowSubTitle2.createCell(15);
        totalScoreCell.setCellValue("积分");
        totalScoreCell.setCellStyle(style);
    }
}
